package com.srd.phone.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shengrongdai.srd.R;
import com.srd.phone.activity.TenderDetailActivity;
import com.srd.phone.bean.TenderInfo;
import com.srd.phone.utils.CommonUtils;
import com.srd.phone.utils.views.RoundProgressBar;
import com.srd.phone.utils.views.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class TabHomeAdapter extends BaseAdapter {
    Context context;
    List<TenderInfo> list = new ArrayList<>();

    public TabHomeAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<TenderInfo> list0) {
        list = list0;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).viewType;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        final TenderInfo tenderInfo = list.get(position);
        ViewHolder viewHolder = null;
        if (type == 0) { //表示未满标
            viewHolder = ViewHolder.get(context, convertView, parent, R.layout.fragment_tab_home_item_undone, position);
        } else if (type == 1) { //表示已满标
            viewHolder = ViewHolder.get(context, convertView, parent, R.layout.fragment_tab_home_item_complete, position);
        }
        if (type == 0) { //表示未满标
            TextView tv_undone_tender = viewHolder.getView(R.id.tv_undone_tender);
            tv_undone_tender.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tv_undone_tender.getPaint().setAntiAlias(true);
            tv_undone_tender.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TenderDetailActivity.class);
                    intent.putExtra("info", tenderInfo);
                    context.startActivity(intent);
                }
            });
            TextView tv_tender_name = viewHolder.getView(R.id.tv_tender_name);
            TextView tv_tender_lilv = viewHolder.getView(R.id.tv_tender_lilv); //年利率
            TextView tv_tender_jine = viewHolder.getView(R.id.tv_tender_jine); //借款金额
            TextView tv_tender_time = viewHolder.getView(R.id.tv_tender_time); //借款时间
            TextView tv_tender_huankuan = viewHolder.getView(R.id.tv_tender_huankuan); //还款方式
            RoundProgressBar rpb_tender_complete = viewHolder.getView(R.id.rpb_tender_undone); //进度条
            TextView tv_last_money = viewHolder.getView(R.id.tv_last_money); //剩余可投

            tv_tender_name.setText(tenderInfo.nm);
            setTenderTextSpan(tv_tender_lilv, CommonUtils.getDecimalFormatFirst(tenderInfo.anyield), "%");
            if (tenderInfo.moneys < 10000) {
                setTenderTextSpan(tv_tender_jine, tenderInfo.moneys + "", "元");
            } else {
                double d = tenderInfo.moneys / 10000;
                setTenderTextSpan(tv_tender_jine, String.valueOf(d), "万元");
            }
            if (tenderInfo.timetpm.contains("月")) {
                setTenderTextSpan(tv_tender_time, tenderInfo.term, "个" + tenderInfo.timetpm);
            } else {
                setTenderTextSpan(tv_tender_time, tenderInfo.term, tenderInfo.timetpm);
            }
            setTenderTextSpan(tv_tender_huankuan, "", tenderInfo.nnm);
            tv_last_money.setText("剩余可投：￥" + (tenderInfo.moneys - tenderInfo.total));

            rpb_tender_complete.setProgress((int) tenderInfo.subSch);
            rpb_tender_complete.setText("抢\n" + (int) tenderInfo.subSch + "%");
            rpb_tender_complete.setTextColor(Color.parseColor("#bf1829"));
        } else if (type == 1) { //表示已满标
            TextView tv_complete_tender = viewHolder.getView(R.id.tv_complete_tender);
            tv_complete_tender.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tv_complete_tender.getPaint().setAntiAlias(true);
            tv_complete_tender.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TenderDetailActivity.class);
                    intent.putExtra("info", tenderInfo);
                    context.startActivity(intent);
                }
            });
            TextView tv_tender_name = viewHolder.getView(R.id.tv_tender_name);
            TextView tv_tender_complete_lilv = viewHolder.getView(R.id.tv_tender_complete_lilv); //年利率
            TextView tv_tender_complete_time = viewHolder.getView(R.id.tv_tender_complete_time); //满标用时
            TextView tv_tender_complete_money = viewHolder.getView(R.id.tv_tender_complete_money); //投资金额
            TextView tv_tender_complete_peoples = viewHolder.getView(R.id.tv_tender_complete_peoples); //投资人数
            TextView tv_tender_complete_method = viewHolder.getView(R.id.tv_tender_complete_method); //还款方式
            RoundProgressBar rpb_tender_complete = viewHolder.getView(R.id.rpb_tender_complete); //进度条

            tv_tender_name.setText(tenderInfo.nm);
            setTenderTextSpan(tv_tender_complete_lilv, CommonUtils.getDecimalFormatFirst(tenderInfo.anyield));
            tv_tender_complete_time.setText("满标用时:" + getHoursMits(tenderInfo.stime, tenderInfo.emtime));
            if (tenderInfo.moneys < 10000) {
                tv_tender_complete_money.setText("投资金额:" + tenderInfo.moneys + "元");
            } else {
                double d = tenderInfo.moneys / 10000;
                tv_tender_complete_money.setText("投资金额:" + d + "万元");
            }
            tv_tender_complete_peoples.setText("投资人数:" + tenderInfo.count + "人");
            tv_tender_complete_method.setText("还款方式:" + tenderInfo.nnm);
            rpb_tender_complete.setProgress(0);
            rpb_tender_complete.setText("还款中");
            rpb_tender_complete.setTextColor(Color.parseColor("#999999"));
        }
        return viewHolder.getConvertView();
    }

    /**
     * 未满标
     */
    public void setTenderTextSpan(TextView tv, String... strs) {
        String value = strs[0];
        SpannableString sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(20, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.setText(sb);
        value = strs[1];
        sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(12, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.append(sb);
    }

    /**
     * 已满标
     */
    public void setTenderTextSpan(TextView tv, String strs) {
        String value = strs;
        SpannableString sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(20, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.setText(sb);
        value = "%";
        sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(11, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.append(sb);
    }

    /**
     * 计算满标用时
     */
    private String getHoursMits(String start, String end) {
        long sl = CommonUtils.parseTimestamp(start);
        long el = CommonUtils.parseTimestamp(end);
        long minute = (el - sl) / 1000 / 60;
        long hours = minute / 60;
        minute = minute % 60;
        return hours + "小时" + minute + "分钟";
    }
}
